/*
 * UserContactsFrame.java
 *
 * Created on 15 de Janeiro de 2008, 13:14
 */
package netfone;

import java.util.List;
import javax.swing.DefaultListModel;

import javax.swing.JOptionPane;


/**
 *
 * @author  bml
 */
public class UserContactsFrame extends javax.swing.JFrame {

    User user;
    private DefaultListModel contactsListModel = new DefaultListModel();

    /** Creates new form UserContactsFrame */
    public UserContactsFrame(User user) {
       UserService.getService().loadUser(user);
       this.user= user;
        this.setAlwaysOnTop(true);
        initComponents();
        bindUserContacts();

    }

    private void bindUserContacts() {
        contactsListModel = new DefaultListModel();
       // if (user.getContacts() != null) {
            for (Contact contact : user.getContacts()) {
                contactsListModel.addElement(contact);
            }
       // }
        contactsList.setModel(contactsListModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        contactsList = new javax.swing.JList();
        addContactButton = new javax.swing.JButton();
        removeContact = new javax.swing.JButton();
        savebutton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(netfone.NetFoneApp.class).getContext().getResourceMap(UserContactsFrame.class);
        userLabel.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        userLabel.setText(user.getName());
        userLabel.setName("userLabel"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        contactsList.setModel(contactsListModel);
        contactsList.setName("contactsList"); // NOI18N
        jScrollPane1.setViewportView(contactsList);

        addContactButton.setBackground(resourceMap.getColor("addContactButton.background")); // NOI18N
        addContactButton.setIcon(resourceMap.getIcon("addContactButton.icon")); // NOI18N
        addContactButton.setText(resourceMap.getString("addContactButton.text")); // NOI18N
        addContactButton.setName("addContactButton"); // NOI18N
        addContactButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addContactButtonActionPerformed(evt);
            }
        });

        removeContact.setBackground(resourceMap.getColor("removeContact.background")); // NOI18N
        removeContact.setIcon(resourceMap.getIcon("removeContact.icon")); // NOI18N
        removeContact.setText(resourceMap.getString("removeContact.text")); // NOI18N
        removeContact.setName("removeContact"); // NOI18N
        removeContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeContactActionPerformed(evt);
            }
        });

        savebutton.setBackground(resourceMap.getColor("savebutton.background")); // NOI18N
        savebutton.setIcon(resourceMap.getIcon("savebutton.icon")); // NOI18N
        savebutton.setText(resourceMap.getString("savebutton.text")); // NOI18N
        savebutton.setName("savebutton"); // NOI18N
        savebutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                savebuttonActionPerformed(evt);
            }
        });

        jButton1.setBackground(resourceMap.getColor("jButton1.background")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(savebutton, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(removeContact, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(addContactButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(userLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addContactButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeContact, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(savebutton, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void addContactButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addContactButtonActionPerformed
        String contactNumber = JOptionPane.showInputDialog(this, "Digite o numero de telefone do seu contato");       // TODO add your handling code here:
        String contactName = JOptionPane.showInputDialog(this, "Digite o nome do seu contato");       // TODO add your handling code here:
        Contact contact = new Contact(contactNumber);
        if (contactName != null && !contactName.trim().equals("")) {
            contact.setLabel(contactName);
        }
        user.addContact(contact);
        bindUserContacts();
    }//GEN-LAST:event_addContactButtonActionPerformed

    private void savebuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savebuttonActionPerformed
        int option = JOptionPane.showConfirmDialog(this, "Todas as informacoes alteradas serão salvas. Tem certeza??");
        if (option == JOptionPane.YES_OPTION) {
            try {
                UserService.getService().saveUser(user);
                JOptionPane.showMessageDialog(this, "Dados salvos com sucesso!");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "ERRO!!! Não foi possivel salvar suas alterações");
            }
        }
    }//GEN-LAST:event_savebuttonActionPerformed

    private void removeContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeContactActionPerformed
        Contact contact = (Contact) contactsList.getSelectedValue();
        user.removeContact(contact);
        bindUserContacts();
    }//GEN-LAST:event_removeContactActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        List<Contact> contacts =  UserService.getService().getAllContacts();
        for(Contact c : contacts){
            System.out.println("Contato:: " +  c);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new UserContactsFrame(new User("Teste")).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addContactButton;
    private javax.swing.JList contactsList;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton removeContact;
    private javax.swing.JButton savebutton;
    private javax.swing.JLabel userLabel;
    // End of variables declaration//GEN-END:variables
}
